package com.godyao.mall.pms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.godyao.mall.pms.entity.AttrAttrgroupRelationEntity;
import com.godyao.mall.pms.entity.AttrGroupEntity;
import com.godyao.mall.pms.entity.CategoryEntity;
import com.godyao.mall.pms.service.AttrAttrgroupRelationService;
import com.godyao.mall.pms.service.AttrGroupService;
import com.godyao.mall.pms.service.CategoryService;
import com.godyao.mall.pms.vo.request.AttrRequestVo;
import com.godyao.mall.pms.vo.response.AttrResponseVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.godyao.mall.core.utils.PageUtils;
import com.godyao.mall.core.utils.Query;

import com.godyao.mall.pms.dao.AttrDao;
import com.godyao.mall.pms.entity.AttrEntity;
import com.godyao.mall.pms.service.AttrService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrService")
@RequiredArgsConstructor
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
    private final AttrAttrgroupRelationService attrAttrgroupRelationService;

    private final AttrGroupService attrGroupService;

    private final CategoryService categoryService;


    @Override
    public void updateAttr(AttrRequestVo attr) {
        final AttrEntity attrEntity = new AttrEntity();
        BeanUtil.copyProperties(attr, attrEntity);
        this.updateById(attrEntity);
        // 新增或者更新分组信息
        final Long attrGroupId = attr.getAttrGroupId();
        final Long count = attrAttrgroupRelationService.lambdaQuery()
                .eq(attrGroupId != null, AttrAttrgroupRelationEntity::getAttrGroupId, attrGroupId)
                .count();
        final AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
        attrAttrgroupRelationEntity.setAttrId(attrEntity.getAttrId());
        attrAttrgroupRelationEntity.setAttrGroupId(attrGroupId);
        if (count != null && count > 0) {
            // 更新
            attrAttrgroupRelationService.lambdaUpdate()
                    .set(AttrAttrgroupRelationEntity::getAttrGroupId, attrGroupId)
                    .eq(AttrAttrgroupRelationEntity::getAttrId, attr.getAttrId())
                    .update();
        } else {
            // 新增
            attrAttrgroupRelationService.save(attrAttrgroupRelationEntity);
        }
    }

    @Override
    public AttrResponseVo getDetailById(Long attrId) {
        final AttrResponseVo attrResponseVo = new AttrResponseVo();
        final AttrEntity attrEntity = this.getById(attrId);
        BeanUtil.copyProperties(attrEntity, attrResponseVo);
        final AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationService.lambdaQuery()
                .eq(AttrAttrgroupRelationEntity::getAttrId, attrId)
                .one();
        if (attrAttrgroupRelationEntity != null) {
            final Long attrGroupId = attrAttrgroupRelationEntity.getAttrGroupId();
            final AttrGroupEntity attrGroupEntity = attrGroupService.getById(attrGroupId);
            attrResponseVo.setGroupName(attrGroupEntity.getAttrGroupName());
            attrResponseVo.setAttrGroupId(attrGroupEntity.getAttrGroupId());
            final Long[] pathById = categoryService.getPathById(attrGroupEntity.getCatelogId());
            attrResponseVo.setCatelogPath(pathById);
        }
        return attrResponseVo;
    }

    @Override
    public PageUtils queryDetail(String attrType, Long categoryId, Map<String, Object> params) {
        Integer attrTypeFlag = "base".equalsIgnoreCase(attrType)?1:0;
        final String key = (String) params.get("key");
        final boolean notBlank = StrUtil.isNotBlank(key);
        final IPage<AttrEntity> page = this.lambdaQuery()
                .eq(ObjectUtil.isNotNull(categoryId), AttrEntity::getCatelogId, categoryId)
                .eq(AttrEntity::getAttrType, attrTypeFlag)
                .and(notBlank, wrapper -> wrapper
                        .like(AttrEntity::getAttrName, key)
                        .or()
                        .eq(AttrEntity::getAttrId, key)
                ).page(new Query<AttrEntity>().getPage(params));
        final List<AttrResponseVo> collect = page.getRecords().parallelStream()
                .map(attrEntity -> {
                    final AttrResponseVo attrResponseVo = new AttrResponseVo();
                    BeanUtil.copyProperties(attrEntity, attrResponseVo);
                    if (attrTypeFlag == 1) {
                        final AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationService.lambdaQuery()
                                .eq(AttrAttrgroupRelationEntity::getAttrId, attrEntity.getAttrId())
                                .one();

                        if (ObjectUtil.isNotNull(attrAttrgroupRelationEntity)) {
                            attrResponseVo.setGroupName(attrGroupService.getById(attrAttrgroupRelationEntity.getAttrGroupId()).getAttrGroupName());
                        }
                    }

                    final CategoryEntity categoryEntity = categoryService.getById(attrEntity.getCatelogId());
                    if (ObjectUtil.isNotNull(categoryEntity)) {
                        attrResponseVo.setCatelogName(categoryEntity.getName());
                    }
                    return attrResponseVo;
                }).collect(Collectors.toList());
        final PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(collect);
        return pageUtils;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveDetail(AttrRequestVo attr) {
        final AttrEntity attrEntity = new AttrEntity();
        BeanUtil.copyProperties(attr, attrEntity);
        this.save(attrEntity);
        if (attr.getAttrType().equals(1)) {
            final AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrGroupId(attr.getAttrGroupId());
            attrAttrgroupRelationEntity.setAttrId(attrEntity.getAttrId());
            attrAttrgroupRelationService.save(attrAttrgroupRelationEntity);
        }

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

}
